<?php

if (!defined('BASEPATH'))
    exit('Tidak Diperkenankan mengakses langsung');
/* Class  Model : city
 * di Buat oleh Diar PHP Generator
 * Update List untuk grid karena program generatorku lom sempurna ya hehehehehe */

class modelcity extends CI_Model {

    function __construct() {
        parent::__construct();
    }

    function getArrayListcity() { /* spertinya perlu lock table */
        $xBuffResul = array();
        $xStr = "SELECT " .
                "idx," .
                "idstate," .
                "city," .
                "code," .
                "datein," .
                "idpegawai" .
                " FROM city   order by idx ASC ";
        $query = $this->db->query($xStr);
        foreach ($query->result() as $row) {
            $xBuffResul[$row->idx] = $row->idstate;
        }
        return $xBuffResul;
    }

    function getListcity($xAwal, $xLimit, $xSearch = '') {
        if (!empty($xSearch)) {
            $xSearch = "Where idstate like '%" . $xSearch . "%'";
        }
        $xStr = "SELECT " .
                "idx," .
                "idstate," .
                "city," .
                "code," .
                "datein," .
                "idpegawai" .
                " FROM city $xSearch order by idx DESC limit " . $xAwal . "," . $xLimit;
        $query = $this->db->query($xStr);
        return $query;
    }

        function getListcitybyidstate($xAwal, $xLimit,$xidstate, $xSearch = '') {
        if (!empty($xSearch)) {
            $xSearch = "Where (city like '%" . $xSearch . "%') and idstate = '".$xidstate."'";
        }else{
           $xSearch = "Where idstate = '".$xidstate."'"; 
        }
        $xStr = "SELECT " .
                "idx," .
                "idstate," .
                "city," .
                "code," .
                "datein," .
                "idpegawai" .
                " FROM city $xSearch order by idx DESC limit " . $xAwal . "," . $xLimit;
        $query = $this->db->query($xStr);
        
        return $query;
    }

     function getoptionstatebyidstate($xidstate) {
        $xBufResult = '<option value="0">~ State  ~</option>';
        
        
            $xStr = "SELECT " .
                "idx," .
                "idstate," .
                "city," .
                "code," .
                "datein," .
                "idpegawai" .
                " FROM city WHERE idstate ='".$xidstate."' order by city ASC";
        
        $xQuery = $this->db->query($xStr);
        foreach ($xQuery->result() as $row) {
            
            $xBufResult .= '<option value="' . $row->idx. '" style="">' . $row->city . '</option>';
        }

        return $xBufResult;
    }
    
    function getDetailcity($xidx) {
        
        $xStr = "SELECT " .
                "idx," .
                "idstate," .
                "city," .
                "code," .
                "datein," .
                "idpegawai" .
                " FROM city  WHERE idx = '" . $xidx . "'";

        $query = $this->db->query($xStr);
        $row = $query->row();
        return $row;
    }

    function getLastIndexcity() { /* spertinya perlu lock table */
        $xStr = "SELECT " .
                "idx," .
                "idstate," .
                "city," .
                "code," .
                "datein," .
                "idpegawai" .
                " FROM city order by idx DESC limit 1 ";
        $query = $this->db->query($xStr);
        $row = $query->row();
        return $row;
    }

    Function setInsertcity($xidx, $xidstate, $xcity, $xcode, $xdatein, $xidpegawai) {
        $xStr = " INSERT INTO city( " .
                "idx," .
                "idstate," .
                "city," .
                "code," .
                "datein," .
                "idpegawai) VALUES('" . $xidx . "','" . $xidstate . "','" . $xcity . "','" . $xcode . "',now(),'" . $xidpegawai . "')";
        $query = $this->db->query($xStr);
        return $xidx;
    }

    Function setUpdatecity($xidx, $xidstate, $xcity, $xcode, $xdatein, $xidpegawai) {
        $xStr = " UPDATE city SET " .
                "idx='" . $xidx . "'," .
                "idstate='" . $xidstate . "'," .
                "city='" . $xcity . "'," .
                "code='" . $xcode . "'," .
//                "datein='" . $xdatein . "'," .
                "idpegawai='" . $xidpegawai . "' WHERE idx = '" . $xidx . "'";
        $query = $this->db->query($xStr);
        return "Untuk test Revert Modification".$xidx;
    }

    function setDeletecity($xidx) {
        $xStr = " DELETE FROM city WHERE city.idx = '" . $xidx . "'";

        $query = $this->db->query($xStr);
        $this->setInsertLogDeletecity($xidx);
    }

    function setInsertLogDeletecity($xidx) {
        $xidpegawai = $this->session->userdata('idpegawai');
        $xStr = "insert into logdelrecord(idxhapus,nmtable,tgllog,ideksekusi) values($xidx,'city',now(),$xidpegawai)";
        $query = $this->db->query($xStr);
    }

}

?>